package com.jiudengnile.liushu.web.action.crm;


import java.util.List;

import wint.help.biz.result.Result;
import wint.lang.convert.ConvertUtil;
import wint.lang.utils.CollectionUtil;
import wint.lang.utils.StringUtil;
import wint.mvc.flow.FlowData;
import wint.mvc.form.Form;
import wint.mvc.module.annotations.Action;
import wint.mvc.template.Context;

import com.jiudengnile.liushu.biz.ao.PermissionAO;
import com.jiudengnile.liushu.biz.domains.permission.PermissionDO;
import com.jiudengnile.liushu.web.common.base.BaseAction;

public class Permission extends BaseAction {
	
	private PermissionAO permissionAO;
	
	@SuppressWarnings("unchecked")
	@Action(defaultTarget="/crm/permission/addUserToPermission")
	public void doAddUserToPermission(FlowData flowData, Context context) {
		Form form = flowData.getForm("permission.addUserToPermission");
		if (!form.validate()) {
			return;
		}
		String nicks = form.getFields().get("nicks").getValue();
		String permissionIdStr = form.getFields().get("permissionId").getValue();
		long permissionId = ConvertUtil.toLong(permissionIdStr, 0);
		Result result = permissionAO.addUserToPermission(permissionId, nicks);
		if (result.isSuccess()) {
			List<String> notExistNicks = (List<String>)result.getModels().get("notExistNicks");
			if (CollectionUtil.isEmpty(notExistNicks)) {
				flowData.redirectTo("adminModule", "permission/permissionUserList").param("permissionId", permissionId);
			} else {
				flowData.redirectTo("adminModule", "permission/permissionUserList").param("permissionId", permissionId).param("notExistNicks", CollectionUtil.join(notExistNicks, ";"));
			}
			
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	public void addUserToPermission(FlowData flowData, Context context) {
		long permissionId = flowData.getParameters().getLong("permissionId");
		Result result = permissionAO.viewAddUserToPermission(permissionId);
		this.handleResult(result, flowData, context);
	}
	
	public void permissionUserList(FlowData flowData, Context context) {
		int page = flowData.getParameters().getInt("page", 1);
		long permissionId = flowData.getParameters().getLong("permissionId");
		String notExistNicks = flowData.getParameters().getString("notExistNicks");
		Result result = permissionAO.permissionUserList(page, permissionId);
		if (!StringUtil.isEmpty(notExistNicks)) {
			result.getModels().put("notExistNicks", notExistNicks);
		}
		this.handleResult(result, flowData, context);
	}
	
	public void list(FlowData flowData, Context context) {
		int page = flowData.getParameters().getInt("page", 1);
		Result result = permissionAO.listPermissions(page);
		this.handleResult(result, flowData, context);
	}

	@Action(defaultTarget="/crm/permission/create")
	public void doCreate(FlowData flowData, Context context) {
		Form form = flowData.getForm("permission.create");
		PermissionDO permissionDO = new PermissionDO();
		if (!form.apply(permissionDO)) {
			return;
		}
		Result result = permissionAO.createPermission(permissionDO);
		if (result.isSuccess()) {
			flowData.redirectTo("adminModule", "permission/list");
		} else {
			this.handleError(result, flowData, context);
		}
	}
	
	public void setPermissionAO(PermissionAO permissionAO) {
		this.permissionAO = permissionAO;
	}

}
